package com.example.demo;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

public class WebViewWithJSActivity extends AppCompatActivity {
  private Button js_alert;
  private WebView webViewWithJS;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_with_js);

        js_alert = findViewById(R.id.js_alert);
        webViewWithJS = findViewById(R.id.webViewWithJS);

        webViewWithJS.loadUrl("file:///android_asset/test.html");

        webViewWithJS.getSettings().setJavaScriptEnabled(true);
        webViewWithJS.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webViewWithJS.getSettings().setDefaultTextEncodingName("UTF-8");

        webViewWithJS.addJavascriptInterface(new JStoAndroid(WebViewWithJSActivity.this), "test");


        webViewWithJS.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                AlertDialog alertDialog = new AlertDialog.Builder(WebViewWithJSActivity.this)
                        .setTitle("JS alert")
                        .setMessage(message)
                        .setCancelable(true)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                webViewWithJS.setFocusable(true);

                            }
                        })
                        .setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                                dialog.cancel();
                                webViewWithJS.requestFocus();
                            }
                        })
                        .create();
                alertDialog.show();
                return true;
            }
        });
        js_alert.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                //第一种方法：
               /* webViewWithJS.loadUrl("javaScript:myFunction()");*/
                //第二种方法：
                webViewWithJS.evaluateJavascript("javaScript:myFunction()", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {

                    }
                });
            }
        });




    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("ssss","sssss");
        return true;
    }
}
